#!/usr/bin/env node

/*global someFunction, args*/
/*eslint no-undef: "error"*/

const argv = process.argv.slice(2);

// 检查参数数量
if(argv.length !== 2) {
  console.error('error');
  process.exit(1);
}

// 解析参数
const pid = parseInt(args[0], 10);
const signal = args[1];

// 检查进程 ID 是否为有效数字
if (isNaN(pid)) {
  console.error('Error: PID must be a valid number');
  process.exit(1);
}

// 检查信号量是否有效
const validSignals = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGUSR1', 'SIGUSR2'];
if (!validSignals.includes(signal)) {
  console.error(`Error: Invalid signal. Valid signals are: ${validSignals.join(', ')}`);
  process.exit(1);
}

// 向指定进程 ID 发送信号
try {
  process.kill(pid, signal);
  console.log(`Sent ${signal} to process ${pid}`);
} catch (error) {
  console.error(`Error: Failed to send signal to process ${pid}: ${error.message}`);
  process.exit(1);
}
